﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using DaoHelper.Religion;
using DaoHelper.Religion.Entity;
using WB.Library;
public partial class admin_Modules_Category : WB.Controls.UcControls
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCombo();
            LoadParent();
        }
    }

    void LoadCombo()
    {
        ddlCatParent.DataTextField = "CatName";
        ddlCatParent.DataValueField = "Id";
        ddlCatParent.DataSource = RelCategoriesDao.Instants().GetParentAll();
        ddlCatParent.DataBind();
    }

    void LoadParent()
    {
        repParent.DataSource = RelCategoriesDao.Instants().GetParentAll();
        repParent.DataBind();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        RelCategoriesEntity ob = RelCategoriesDao.Instants().SelectOne(FGuid.ToGuid(hidId.Value));
        if (ob == null)
        {
            ob = new RelCategoriesEntity();
            ob.CatName = txtCatName.Text;
            ob.CatType = ddlCatType.Text;
            ob.ParentId = FGuid.ToGuid(ddlCatParent.Text);

            ob.CreatedBy = Util.CurrentUserName;
            ob.CreatedDate = DateTime.Now;
            ob.UpdatedBy = Util.CurrentUserName;
            ob.UpdatedDate = DateTime.Now;

            RelCategoriesDao.Instants().Insert(ob);
        }
        else
        {
            ob.CatName = txtCatName.Text;
            ob.CatType = ddlCatType.Text;
            ob.ParentId = FGuid.ToGuid(ddlCatParent.Text);
            ob.UpdatedBy = Util.CurrentUserName;
            ob.UpdatedDate = DateTime.Now;
            RelCategoriesDao.Instants().Update(ob);
        }

        RefreshPage();
    }

    protected void repParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView r = (DataRowView)e.Item.DataItem;
        DataTable tbl = RelCategoriesDao.Instants().GetChild(FGuid.ToGuid(r["Id"].ToString()));
        if (tbl != null && tbl.Rows.Count > 0)
        {
            Repeater rep = (Repeater)e.Item.FindControl("rep");
            rep.DataSource = tbl;
            rep.DataBind();
        }
        else
        {
            e.Item.Visible = false;
        }
    }

    protected void repParent_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        RelCategoriesEntity ob;
        switch (e.CommandName)
        {
            case "edit":
                ob = RelCategoriesDao.Instants().SelectOne(FGuid.ToGuid(e.CommandArgument.ToString()));
                if (ob != null)
                {
                    hidId.Value = ob.Id.ToString();
                    txtCatName.Text = ob.CatName;
                    ddlCatParent.Text = ob.ParentId.ToString();
                    ddlCatType.Text = ob.CatType;

                    btnUpdate.Text = "Cập nhật";
                }
                break;
            case "del":
                ob = RelCategoriesDao.Instants().SelectOne(FGuid.ToGuid(e.CommandArgument.ToString()));
                if (ob != null)
                {
                    ob.UpdatedBy = Util.CurrentUserName;
                    ob.UpdatedDate = DateTime.Now;
                    ob.IsDeleted = true;
                    RelCategoriesDao.Instants().Update(ob);

                    RefreshPage();
                }
                break;
        }
    }
}
